﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Crawler.Model;
using System.Text.RegularExpressions;
using Crawler.BusinessActions;

namespace WinFromsClient
{
    public partial class CopyUrlForm : Form
    {
        private TaskItemInfo taskItem;
        public CopyUrlForm(TaskItemInfo taskItem)
        {
            InitializeComponent();

            this.taskItem = taskItem;

            txtUrlRule.Text = taskItem.Url;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            foreach (string s in txtUrls.Lines)
            {
                TaskItemInfo item = new TaskItemInfo();

                CopyTaskItem(taskItem, item, s, chkRule.Checked);

                taskItem.Task.TaskItems.Add(item);
            }
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            string pattern = "{[^}]+}";

            MatchCollection matches = Utilities.GetMatches(pattern, txtUrlRule.Text);

            StringBuilder sb = new StringBuilder("");

            foreach (Match match in matches)
            {

                string[] segments = match.Value.Trim('{', '}').Split(':');
                string[] se = segments[1].Split('-');

                int start = Int32.Parse(se[0]);
                int end = Int32.Parse(se[1]);

                do
                {
                    string current = Husb.Util.CommonUtil.FillZero(start, Int32.Parse(segments[0]));
                    sb.Append(txtUrlRule.Text.Replace(match.Value, current)).Append(System.Environment.NewLine);
                    start++;

                } while (start <= end);
            }

            txtUrls.Text = sb.ToString();
        }

        private void CopyTaskItem(TaskItemInfo from, TaskItemInfo to, string url, bool isCopyRule)
        {
            to.AutoPaging = from.AutoPaging;
            to.IsNavigation = from.IsNavigation;
            to.NextPage = from.NextPage;
            to.PageCategory = from.PageCategory;
            to.Task = from.Task;
            to.Url = url;

            to.Id = Guid.NewGuid();
            to.TaskId = from.TaskId;

            TaskItem.Insert(to);

            if (isCopyRule)
            {
                foreach (CaptureRuleInfo rule in from.CaptureRules)
                {
                    CaptureRuleInfo newRule = new CaptureRuleInfo();
                    CopyCaptureRule(rule, newRule, to);

                    to.CaptureRules.Add(newRule);
                }

            }
        }

        private void CopyCaptureRule(CaptureRuleInfo from, CaptureRuleInfo to, TaskItemInfo item)
        {
            to.Category = from.Category;
            to.ContentType = from.ContentType;
            to.EndString = from.EndString;
            to.MatchMethod = from.MatchMethod;
            to.MatchRegex = from.MatchRegex;
            to.Name = from.Name;
            to.Prefix = from.Prefix;
            to.ProcessMethod = from.ProcessMethod;
            to.ReplaceRegex = from.ReplaceRegex;
            to.StartString = from.StartString;
            to.Suffix = from.Suffix;
            to.TaskItem = item;
            to.TaskItemId = item.Id;
            to.Url = from.Url;

            to.Id = Guid.NewGuid();

            CaptureRule.Insert(to);
        }
    }
}
